﻿// ----------------------------------------------------------------------
// <copyright file="PropertyNodeGetter.cs" company="Meligo">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------

namespace Meligo.ModuleExtractor.GenerateModuleFactory.Helper
{
    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;
    using Meligo.ModuleExtractor.GenerateModuleFactory.Entities;
    using Microsoft.SharePoint;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class PropertyNodeGetter
    {
        public static XElement GetPropertyNode(string properyName, string propertyValue)
        {
            return new XElement("Property", new object[] { new XAttribute("Name", properyName), new XAttribute("Value", propertyValue ?? string.Empty) });
        }

        public static string GetSiteOrWebRelativeMingledUrl(SPWeb web, string url, Dictionary<string, PathSiteInfo> pathSiteInfoMap)
        {
            string text = web.Site.ServerRelativeUrl.TrimEnd(new char[] { '/' });
            string text2 = web.ServerRelativeUrl.TrimEnd(new char[] { '/' });
            Uri uri = new Uri(web.Url);
            string components = uri.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped);
            string components2 = uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
            string text3 = url;
            Uri uri2;
            string result;
            if (Uri.TryCreate(text3, UriKind.Absolute, out uri2))
            {
                string components3 = uri2.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped);
                if (string.Compare(components3, components, true) != 0)
                {
                    result = url;
                    return result;
                }

                text3 = uri2.PathAndQuery;
            }

            text3 = text3.TrimEnd(new char[] { '/' });
            if (string.Compare(text3, text, true) == 0)
            {
                result = "~SiteCollection/";
            }
            else
            {
                if (string.Compare(text3, text2, true) == 0)
                {
                    result = "~Site/";
                }
                else
                {
                    string text4 = null;
                    string text5 = null;
                    if (text3.StartsWith(text2, StringComparison.OrdinalIgnoreCase) || text3.StartsWith(text2, StringComparison.OrdinalIgnoreCase))
                    {
                        PathSiteInfo pathSiteInfo = PathSiteInfoGetter.GetPathSiteInfo(components2, text3, pathSiteInfoMap);
                        if (web.ID == pathSiteInfo.WebID)
                        {
                            text4 = web.IsRootWeb ? "~SiteCollection/" : "~Site/";
                            text5 = text3.Substring(text2.Length);
                        }
                        else
                        {
                            if (web.Site.ID == pathSiteInfo.SiteID)
                            {
                                text4 = "~SiteCollection/";
                                text5 = text3.Substring(text.Length);
                            }
                        }
                    }

                    result = (text4 == null) ? ("/" + text3.TrimStart(new char[] { '/' })) : (text4 + text5.TrimStart(new char[] { '/' }));
                }
            }

            return result;
        }
    }
}
